package cn.hrc.utils;

import cn.hrc.entity.XJson;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public class PackJson {
    static Logger logger = LoggerFactory.getLogger(PackJson.class);
    static ObjectMapper objectMapper = new ObjectMapper();
    public static Object redisJson(Object obj){
        return null;
    }

    public static XJson pack(int code, String msg, Object data) {
        XJson myJson = new XJson(code, msg, data);
        return myJson;
    }

    public static String packJson(int code,String msg,Object data){
        Map<String,Object> m = new HashMap<>();
        m.put("code",code);
        m.put("msg",msg);
        m.put("data",data);
        // 把 m 转 Json
        String json = "";
        try {
            // String序列化
            json = objectMapper.writeValueAsString(m);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        logger.debug("json=" + json);
        return json;
    }

    // 拼接 JSON 对象（字符串）
    private String pack2(int code,String msg,String data){
        String strJson = "{" +
                "\"" + "code" + "\"" + ":" + code + "," +
                "\"" + "msg" + "\"" + ":" + "\"" + msg + "\"" + "," +
                "\"" + "data" + "\"" + ":" + "\"" + data + "\"" +
                "}";
        return strJson;
    }
}
